﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace web_uchet.Classes.Security
{
    public static class _3DesAlghImplem
    {
        private static byte[] tdesKey
        {
            get
            {
                byte[] key = new byte[24];
                byte[] init = new byte[]
            {123,134,54,3,234,2,134,34,234,23,//4,54,34,5,0,235,44,4,0,234,200,11,234,23,4,234,23,111,42,34,23,119,4,23
            //,234,94,25,4,5,24,23,4,74,23,4,234,23,4,234,23,4,234,23,4,63,23,4,26,23,4,5,43,5,45,4,5,4,66,5,6,56,7,7,
            78,78,2,5,5,8,9,97,67,88,//99,7,9,0,8,9,8,0,89,69,65,67,4,99,45,4,3,4,64,5,6,56,9,34,5,66,56,34,6,45,6,35,4,56,5,47,6,7,9,
            79,89//,76,9,7
            //,54,5,3,45,34,53,6,54,7,65,86,8,6,86,8,56,4,9,3,2,3,1,32,3,5,43,67,79,98,0,89,8,9,79,7,86,54,6,5,44,63,6,56,54,6,54,6,45,65,4,6,
            //4,54,6,9,0,97,78,6,5,6,45,3,42,22//,2,45,4,56,7,7,8,6,5,56,54,3,23,1,1,1,3,5,45,3,3,23,43,34,77,23,43,46,4,56,7,7,88,9,7,8,6,45,
            //45,45,65,4,6,54,56,4,7,56,7,56,65,7,3,6,56,7,3,6,5,87,89,7,2,3,21,23,42,35,47,7,67,98,6,96,7,6,4,4,5,2,31,1,2,5,56,7,45,6,6,6,56,8,6,
            //3,5,34,6,34,6,34,5,45,2,54,1,1,143,4,235,6,5,7,89,8,7,65,54,34,3,5,77,9,7,48,75,6,76,34,41,42,3,5,35,47,56,4,34,5,245,4,35,35,4,75,85,64,6,2
            //,43,5,54,7,4,56,46,7,45,35,4,21,24,2,51,4,243,55,7,66,5,7,32,5,66,5,34,56,7,56,7,65,85,43,3,54,6,36,4,35,35,4,67,64,58,67,5,45,67,5,6,45
            //,43,6,45,75,6,47,56,4,35,4,35,34,6,24,45,41,5,42,3,4,24,36,45,35,6,45,65,4,54,6,54,6,54,6,54,6,54,6,54,64,32,1,23,45,32,14,23,4,35,6,57,45
            //,234,32,4
            };
                init.CopyTo(key, 0);
                string NonDetermArray = (DateTime.Now.ToShortDateString() + "df(*f*&si68fd" +
                    DateTime.Now.Hour.ToString());
                NonDetermArray =Convert.ToBase64String(UnicodeEncoding.UTF8.GetBytes(NonDetermArray.ToCharArray()));
                byte[] b = Convert.FromBase64String(NonDetermArray);
                for (int i = 0; i < key.Length && i < b.Length; i++)
                {
                    key[i] = b[i] != 0 ? b[i] : key[i];
                }
                return key;
            }
        }
        private static byte[] tdesIV
        {
            get { return new byte[] { 4, 88, 24, 7, 0, 2, 61, 39 }; }
        }

        public static string EncryptData(String Data)
        {
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            MemoryStream MS = new MemoryStream();
            ICryptoTransform Encryptor = tdes.CreateEncryptor(_3DesAlghImplem.tdesKey, _3DesAlghImplem.tdesIV);
            CryptoStream encStream =
                new CryptoStream(MS, Encryptor, CryptoStreamMode.Write);
            byte[] toEnc = Convert.FromBase64String(Convert.ToBase64String(UnicodeEncoding.UTF8.GetBytes(Data.ToCharArray())));
            encStream.Write(toEnc, 0, toEnc.Length);
            encStream.Flush();
            encStream.FlushFinalBlock();
            string ResultStr;
            byte[] ResultByte = MS.ToArray();
            ResultStr = Convert.ToBase64String(ResultByte);
            encStream.Close();
            return ResultStr;
        }

        public static string DecryptData(String Data)
        {
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            MemoryStream MS = new MemoryStream();
            ICryptoTransform Encryptor = tdes.CreateDecryptor(_3DesAlghImplem.tdesKey, _3DesAlghImplem.tdesIV);
            CryptoStream encStream =
                new CryptoStream(MS, Encryptor, CryptoStreamMode.Write);
            byte[] toDec = Convert.FromBase64String(Data);
            encStream.Write(toDec, 0, toDec.Length);
            encStream.Flush();
            encStream.FlushFinalBlock();
            string ResultStr;
            byte[] ResultByte = MS.ToArray();
            ResultStr = UnicodeEncoding.UTF8.GetString(Convert.FromBase64String(Convert.ToBase64String(ResultByte)));
            encStream.Close();
            return ResultStr;
        }
    }
}
